package com.xu.cloud.common.aop;

import com.xu.cloud.common.lock.ISingleLock;
import com.xu.cloud.common.lock.SingleLock;
import com.xu.cloud.common.util.ExceptionUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

/**
 * 单机本地锁切面
 *
 * @author xuguan
 * @since 2025/3/21
 */
@Aspect
public class SingleLockAspect {
    private final ISingleLock singleLock;

    public SingleLockAspect(ISingleLock singleLock) {
        this.singleLock = singleLock;
    }


    @Around("@annotation(lockAnnotation)")
    public Object around(ProceedingJoinPoint pjp, SingleLock lockAnnotation) {
        Object ret;

        try {
            singleLock.lock();
            ret = pjp.proceed();
        } catch (Throwable e) {
            throw ExceptionUtil.wrapXuCloudException(e);
        } finally {
            singleLock.unlock();
        }

        return ret;
    }
}